﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfMongoDB.Model;

namespace WpfMongoDB
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        TreeNodeModel treeSelectItem;
        List<TreeNodeModel> myItem = new List<TreeNodeModel>();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void connDB_Click(object sender, RoutedEventArgs e)
        {
            ConnectionForm connf = new ConnectionForm();
            connf.ShowDialog();
            var tnm = connf.Name;
            if (tnm==null)
            {
                return;
            }
            DateTime dt = new DateTime();
            dt.ToLocalTime();
            var item = StaticCs.Servers.Where(w => w.Key == tnm).FirstOrDefault();
            BindTreeData btd = new BindTreeData(this);
            var titem = btd.Bind(item.Key,item.Value);
            myItem.Add(titem);
            treeView.ItemsSource = myItem;
            treeView.Items.Refresh();
        }

        private void exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void treeView_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            TreeViewItem item = sender as TreeViewItem;
            if (item != null)
            {
                item.Focus();
                e.Handled = true;
            }
            treeView.ContextMenu = null;
            if (treeSelectItem!=null)
            {
                BindTreeData btd=new BindTreeData(this);
                switch (treeSelectItem.Type)
                {
                    case TreeNodeType.DataBase:
                        break;
                    case TreeNodeType.Collection:
                        treeView.ContextMenu = btd.BindCollectionMenu(treeSelectItem);
                        break;
                    case TreeNodeType.GFS:
                        break;
                    case TreeNodeType.Javascript:
                        break;
                    case TreeNodeType.Index:
                        break;
                    case TreeNodeType.User:
                        break;
                    default:
                        break;
                }
            }
        }

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            treeSelectItem = e.NewValue as TreeNodeModel;
        }

        private void tabcontrol_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
        }

    }
}
